from card import *

class ScoreMatrix(object):
    def __init__(self,scorefile):
        self.scorefile=scorefile
        self.matrix={}
        f=open(scorefile)
        for l in f:
            items=l[:-1].split(",")
            self.matrix[(items[0],items[1])]=float(items[2])
        f.close()

    def score(self,card1,card2):
        h,o=card1,card2
        if h.__class__ !=Hazard:
            h,o=o,h
        print h
        print o
        if h.__class__!=Hazard or (o.__class__!=Situation and o.__class__!=Action):
            #We're screwed
            raise TypeError("Score only works for Hazard/Action or Hazard/Situation pairs")
        try:
            pairScore=self.matrix[(o.code,h.code)]
        except KeyError:
            raise KeyError("%s does not contain data for the pair %s -  %s"%(self.scorefile,o.code,h.code))
        return pairScore

if __name__=="__main__":
    a=ScoreMatrix("../data/situation_hazard_pairs.csv")
    s=Situation("bath","In a bath","S/2")
    
    h=Hazard("Duck","Big duck","H/W1","duck.png","A challenger has appeared")
    print a.score(s,h)
#print a.matrix
